﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Text.RegularExpressions;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using LocalSettings;
using QuickTasks.ViewModels.Data;
using Microsoft.Advertising.Mobile.UI;

namespace QuickTasks
{
    public partial class AddMessages : PhoneApplicationPage
    {
       
        string Qid = "";
        string type = "";
       
        #region constructor

        public AddMessages()
        {
            InitializeComponent();
         
        }

        #endregion

        #region events
     
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (NavigationContext.QueryString.TryGetValue("type", out type))
                {
                    if (type == "Email")
                    {
                        pvtMessage.SelectedIndex = 0;

                    }
                    if (type == "SMS")
                    {
                        pvtMessage.SelectedIndex = 1;

                    }
                }
                else
                    pvtMessage.SelectedIndex = 0;


                if (!string.IsNullOrEmpty(App.EmailSubject)) tbxSubject.Text = App.EmailSubject;
                if (!string.IsNullOrEmpty(App.EmailMessage)) tbxEmailmessage.Text = App.EmailMessage;




                string Qtype = "";


                if (NavigationContext.QueryString.TryGetValue("quickId", out Qid))
                {

                    if (NavigationContext.QueryString.TryGetValue("quicktype", out Qtype))
                    {
                        if (Qtype == "Email")
                        {
                            App.MessageIdEdit = Qid;
                            pvtMessage.SelectedIndex = 0;
                            List<QuickMessageProp> objmessage = new List<QuickMessageProp>();
                            using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                            {
                                var favpeoplelist = from i in context.message where i.id.ToString() == App.MessageIdEdit select i;
                                foreach (var favpeople in favpeoplelist)
                                {
                                    tbxSubject.Text = favpeople.subject;
                                    App.TrEmailId = favpeople.reciepient;
                                    App.TcEmailId = favpeople.cc;

                                    tbxEmailmessage.Text = favpeople.message;
                                    break;
                                }
                            }
                        }

                        if (Qtype == "SMS")
                        {
                            App.MessageIdEdit = Qid;
                            pvtMessage.SelectedIndex = 1;
                            List<QuickMessageProp> objmessage = new List<QuickMessageProp>();
                            using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                            {
                                var favpeoplelist = from i in context.message where i.id.ToString() == App.MessageIdEdit select i;
                                foreach (var favpeople in favpeoplelist)
                                {
                                    tbxTitle.Text = favpeople.subject;
                                    App.Nmobile = favpeople.reciepient;
                                    tbxMobilemessage.Text = favpeople.message;
                                    break;
                                }
                            }
                        }
                    }
                }






                if (!string.IsNullOrEmpty(App.Subject)) tbxTitle.Text = App.Subject;
                if (App.Message != "") tbxMobilemessage.Text = App.Message;

                IEnumerable<System.Windows.Navigation.JournalEntry> back = NavigationService.BackStack;
                foreach (var b in back)
                {
                    if (b.Source.ToString().Contains("AddMessages.xaml"))
                        NavigationService.RemoveBackEntry();
                    break;
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_Loaded event In AddMessages.xaml page", ex);
            }
        }

        private void imgEmailIdSelection_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            App.EmailSubject = tbxSubject.Text;
          
            App.EmailMessage = tbxEmailmessage.Text;
            string type = "Rec";
            NavigationService.Navigate(new Uri("/Views/FavoritePeople/FavoriteEmails.xaml?type="+type, UriKind.Relative));
        }
        private void imgEmailIdSelection_MouseLeftButtonDown1(object sender, MouseButtonEventArgs e)
        {
            App.EmailSubject = tbxSubject.Text;
            App.EmailMessage = tbxEmailmessage.Text;
          
            string type = "cc";
            NavigationService.Navigate(new Uri("/Views/FavoritePeople/FavoriteEmails.xaml?type="+type, UriKind.Relative));
        }

        private void imgMobileSelection_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            App.Subject = tbxTitle.Text;
            App.Message = tbxMobilemessage.Text;
            NavigationService.Navigate(new Uri("/Views/FavoritePeople/FavoriteMobile.xaml", UriKind.Relative));
        }

      

       

        #endregion
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            App.EmailId =string.Empty;
            App.Mobile = string.Empty;
            App.Subject = string.Empty;
            App.Message = string.Empty;
            App.Nmobile = string.Empty;
            App.TrEmailId = string.Empty;
            App.EmailMessage = string.Empty;
            App.EmailSubject = string.Empty;
            App.TcEmailId = string.Empty;
        }
        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (type == "Email")
                {
                    if (string.IsNullOrEmpty(App.EmailId))
                    {
                        tblkRecipientValidation.Visibility = Visibility.Visible;
                        return;
                    }
                    else
                        tblkRecipientValidation.Visibility = Visibility.Collapsed;
                    if (string.IsNullOrEmpty(tbxSubject.Text))
                    {
                        tblkSubjectValidation.Visibility = Visibility.Visible;
                        return;
                    }
                    else
                        tblkSubjectValidation.Visibility = Visibility.Collapsed;

                    if (string.IsNullOrEmpty(tbxEmailmessage.Text))
                    {
                        tblkMessageValidation.Visibility = Visibility.Visible;
                        return;
                    }
                    else
                        tblkMessageValidation.Visibility = Visibility.Collapsed;


                    QuickMessages objQuickMessages = new QuickMessages();
                    objQuickMessages.SaveMessages(tbxSubject.Text, App.TcEmailId, App.TrEmailId, tbxEmailmessage.Text, "Email", App.MessageIdEdit);
                    NavigationService.GoBack();
                    App.EmailId = string.Empty;
                    App.Mobile = string.Empty;
                    App.Subject = string.Empty;
                    App.Message = string.Empty;
                    App.Nmobile = string.Empty;
                    App.TrEmailId = string.Empty;
                    App.EmailMessage = string.Empty;
                    App.EmailSubject = string.Empty;
                    App.TcEmailId = string.Empty;
                }
                if (type == "SMS")
                {
                    if (string.IsNullOrEmpty(App.Mobile))
                    {
                        tblkRecipientmobileValidation.Visibility = Visibility.Visible;
                        return;
                    }
                    else
                        tblkRecipientmobileValidation.Visibility = Visibility.Collapsed;
                    if (string.IsNullOrEmpty(tbxTitle.Text))
                    {
                        tblkTitleValidation.Visibility = Visibility.Visible;
                        return;
                    }
                    else
                        tblkTitleValidation.Visibility = Visibility.Collapsed;



                    if (string.IsNullOrEmpty(tbxMobilemessage.Text))
                    {
                        tblkMessagemobileValidation.Visibility = Visibility.Visible;
                        return;
                    }
                    else
                        tblkMessagemobileValidation.Visibility = Visibility.Collapsed;


                    QuickMessages objQuickMessages = new QuickMessages();
                    objQuickMessages.SaveMessages(tbxTitle.Text, "", App.Nmobile, tbxMobilemessage.Text, "SMS", App.MessageIdEdit);
                    NavigationService.GoBack();
                    App.EmailId = "";
                    App.Mobile = "";
                    App.Subject = "";
                    App.Message = "";
                    App.EmailSubject = "";
                    App.EmailMessage = "";
                    App.Nmobile = "";
                    App.TrEmailId = "";
                    App.TcEmailId = "";
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in  ApplicationBarIconButton_Click event In AddMessages.xaml page", ex);
            }
        }

       
        
    }
}
